// src/main/java/org/example/backend/repository/EvaluationRepository.java
package org.example.backend.repository;

import org.example.backend.entity.Evaluation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EvaluationRepository extends JpaRepository<Evaluation, Long> {
    Page<Evaluation> findByStudentId(Long studentId, Pageable pageable);
    Page<Evaluation> findByInternshipId(Long internshipId, Pageable pageable);
    Page<Evaluation> findByEvaluatorTypeAndEvaluatorId(Integer evaluatorType, Long evaluatorId, Pageable pageable);
    List<Evaluation> findByInternshipId(Long internshipId);

    boolean existsByInternshipIdAndEvaluatorTypeAndEvaluatorId(Long internshipId, Integer evaluatorType, Long evaluatorId);
    boolean existsByInternshipIdAndEvaluatorType(Long internshipId, Integer evaluatorType);

    @Query("SELECT CASE WHEN COUNT(e) > 0 THEN true ELSE false END FROM Evaluation e WHERE e.internshipId = :internshipId AND e.evaluatorType = 1 AND e.evaluatorId = :teacherId")
    boolean existsByInternshipIdAndTeacherId(@Param("internshipId") Long internshipId, @Param("teacherId") Long teacherId);
}